package com.donzell.controller;

import java.util.List;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.donzell.dao.UserRepository;
import com.donzell.model.bean.Demo;
import com.donzell.model.bean.User;
@Controller
@RequestMapping("test")
public class TestController {
    
    @RequestMapping(value="/test",produces="text/plain;charset=UTF-8")
    public String test(HttpSession httpSession,ModelMap map) {
		return "test";
    }
    
    @RequestMapping(value="/test1",produces="text/plain;charset=UTF-8")
    @ResponseBody
    public String tes2t() {
    	return "你好";
    }
	
    @RequestMapping("/valid")
    public String demo(Model model){
       model.addAttribute("demo",new Demo());
       return "test/valid";
    }

    @RequestMapping("/demoAdd")
    public String demoAdd(@Valid Demo demo,BindingResult result,Model model){
       //有错误信息.
       model.addAttribute("demo",demo);
       if(result.hasErrors()){
           List<ObjectError>  list = result.getAllErrors();
           	for(ObjectError  error:list){
           		model.addAttribute("checkerr",list);
           		System.out.println(error.getCode()+"---"+error.getArguments()+"---"+error.getDefaultMessage());
           }
       }
       return "test/valid";
    }
	
}
